<?php
# command line utility
# backend configure
# Step 1, read file mysql-sample.php
$content = file_get_contents('./api/mysql-sample.php');
if(file_exists('config.json')){
    $json_str = file_get_contents('config.json');
    $json = json_decode($json_str);
}
else{
    $json = Null;
}
# Step 2, replace the file content
$content = str_replace('数据库地址', 'localhost', $content);
$content = str_replace('用户名', 'fish', $content);
$content = str_replace('密码', 'fish', $content);
$content = str_replace('数据库名称', 'fish', $content);
$content = str_replace('表前缀', 'utsz', $content);
if($json != Null){
    $content = str_replace('微信小程序appid', $json->appid, $content);
    $content = str_replace('微信小程序appsecret', $json->appsecret, $content);
}
# Step 3, write the content to mysql.php
file_put_contents('./api/mysql.php', $content);

# frontend configure, default is localhost development
$content = file_get_contents('./src/app-config.js');
if($json != Null){
    $content = str_replace('Your Version', $json->version, $content);
    $content = str_replace('Your CDNUrl', $json->cdnurl, $content);
    $content = str_replace('Your ServerUrl', $json->serverurl, $content);        
}
else{
    $content = str_replace('Your Version', '1.0', $content);
    $content = str_replace('Your CDNUrl', 'http://localhost', $content);
    $content = str_replace('Your ServerUrl', 'http://localhost', $content);    
}
file_put_contents('./src/app.js', $content);

$content = file_get_contents('./src/project.config-config.json');
if($json != Null){
    $content = str_replace('Your appid', $json->appid, $content);
}
else{
    $content = str_replace('Your appid', '', $content);
}
file_put_contents('./src/project.config.json', $content);

?>